package com.yumeng.common.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

/**
 *
 * @author wxd
 * @date 2025/9/23 16:52
 */
public class HttpUtils {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    /**
     * 是POST操作
     * @param request
     * @return
     */
    public static boolean isPost(HttpServletRequest request){
        return request.getMethod().equalsIgnoreCase(HttpMethod.POST.name());
    }

    /**
     * 是GET操作
     * @param request
     * @return
     */
    public static boolean isGet(HttpServletRequest request){
        return request.getMethod().equalsIgnoreCase(HttpMethod.GET.name());
    }

    /**
     * 是json媒体类型
     * @param request
     * @return
     */
    public static boolean isJsonMediaType(HttpServletRequest request){
        return MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(request.getContentType());
    }

}
